package view;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import rmi.ClienteRMI;
import bean.Cliente;


public class PanelTablaClientes extends JPanel {

	public PanelTablaClientes() {
        String[] columnNames = { "Id", "Nombre", "Descuento" };
        
        List<ClienteView> clientesView = listarClientesView();
        
        Object[][] data = new Object[clientesView.size()][3];
        int i = 0;
        for (ClienteView cli : clientesView) {
            data[i][0] = cli.getId();
            data[i][1] = cli.getNombre();
            data[i][2] = cli.getDescuento();
            i++;
        }

        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(450,300));
        table.setFillsViewportHeight(true);
        
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);
        
        //Add the scroll pane to this panel.
        add(scrollPane);
        scrollPane.setBounds(0, 0, 100, 100);
    }
        
    public List<ClienteView> listarClientesView(){
    	try{
            List<Cliente> lista = ClienteRMI.getInstance().getIRemota().getClientes();
            List<ClienteView> listaClientesView = new ArrayList<ClienteView>();  
            
            for (Cliente c : lista) {
                    ClienteView cv = new ClienteView(c.getId(), c.getNombre(), c.getDescuento());
                    listaClientesView.add(cv);
            }
            return listaClientesView; 
    	}catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
    	} 
    }
}